import {
  buildModuleUrl,
  ImageryLayer,
  TileMapServiceImageryProvider,
  Viewer,
  Camera,
} from "cesium";
import * as Cesium from "cesium";
import type { ClockOptions } from "../interfaces/core";
import { Rectangle } from "cesium";
import { SingleTileImageryProvider } from "cesium";
// 设置cesium 相机的默认视角范围到中国🇨🇳
Camera.DEFAULT_VIEW_RECTANGLE = Cesium.Rectangle.fromDegrees(25, -15, -180, 85);
interface ViewerConstructorOptions extends Viewer.ConstructorOptions {
  defaultView: { west: number, south: number, east: number, north: number },
  baseLayerUrl: string,
}
/**
 * CesiumViewer 类用于管理和控制 Cesium 3D 地球视图
 * 提供了基础的视图初始化和销毁功能
 * @class
 */
export class CesiumViewer {
  /** Cesium Viewer 实例 */
  viewer: Viewer | null = null;

  startTime: number = new Date().getTime();
  endTime: number = new Date().setHours(2);

  /**
   * 创建 CesiumViewer 实例
   * @param {string} container - 用于承载 Cesium 视图的 DOM 元素
   * @param {ViewerConstructorOptions} [options] - Cesium Viewer 的配置选项
   */
  constructor(
    container: string | HTMLElement,
    options?: ViewerConstructorOptions
  ) {
    this.init(container, options);
  }

  /**
   * 初始化 Cesium Viewer
   * @private
   * @param {string} container - 用于承载 Cesium 视图的 DOM 元素
   * @param {ViewerConstructorOptions} [options] - Cesium Viewer 的配置选项
   */
  private init(
    container: string | HTMLElement,
    options?: ViewerConstructorOptions & {
      defaultView: { west: number, south: number, east: number, north: number }
    }
  ) {

    // ✅ 容器解析：支持传入容器元素或容器 ID
    const element =
      typeof container === "string" ? document.getElementById(container) : container;
    if (!element) throw new Error("CesiumViewer 初始化失败：未找到有效容器元素。");
    // ✅ 相机默认视角：支持传入 Rectangle 对象或 { west, south, east, north } 格式
    const rectangle =
      options?.defaultView instanceof Rectangle
        ? options?.defaultView
        : Rectangle.fromDegrees(
          options?.defaultView?.west || 70,
          options?.defaultView?.south || 10,
          options?.defaultView?.east || 140,
          options?.defaultView?.north || 55
        );

    Camera.DEFAULT_VIEW_RECTANGLE = rectangle;
    Camera.DEFAULT_VIEW_FACTOR = 2;

    // ✅ 构建基础图层：支持单张图片作为底图，默认覆盖全球范围
    const baseLayer = new ImageryLayer(
      new SingleTileImageryProvider({
        url: options?.baseLayerUrl || "/static/world.topo.bathy.200412.3x5400x2700.png",
        rectangle: Rectangle.fromDegrees(-180, -90, 180, 90),
        tileWidth: 5400,
        tileHeight: 2700,
      })
    );
    this.viewer = new Viewer(container, {
      animation: false,
      timeline: false,
      baseLayerPicker: false,
      fullscreenButton: false,
      vrButton: false,
      geocoder: false,
      homeButton: false,
      infoBox: false,
      sceneModePicker: false,
      selectionIndicator: false,
      navigationHelpButton: false,
      navigationInstructionsInitiallyVisible: false,
      baseLayer,
      ...options,
    });
    // this.viewer.scene.light = new Cesium.DirectionalLight({
    //   direction: new Cesium.Cartesian3(0.354925, -0.890918, -0.283358)
    // })
    this.viewer.clock;
  }

  /**
   * 销毁 Cesium Viewer 实例
   * 释放资源并清理内存
   */
  public destroy() {
    if (this.viewer) {
      this.viewer.destroy();
      this.viewer = null;
    }
  }
  /**
   *
   * @param options
   * @returns
   */
  setClock(options: ClockOptions) {
    if (!options) return;

    const { startTime, endTime, speedMultiplier, loop } = options;
    this.viewer!.clock.startTime = Cesium.JulianDate.fromDate(
      new Date(startTime)
    );
    this.viewer!.clock.stopTime = Cesium.JulianDate.fromDate(new Date(endTime));

    // 2. 设置播放参数
    this.viewer!.clock.multiplier = speedMultiplier || 1.0;
    this.viewer!.clock.clockRange = Cesium.ClockRange.LOOP_STOP;

    // 3. 启动动画
    this.viewer!.clock.shouldAnimate = true;
  }
  /**
   *
   * @param mixin
   * @param options
   */
  addExtend(mixin: Viewer.ViewerMixin, options?: any) {
    this.viewer!.extend(mixin, options);
  }
}
